﻿namespace Commander.Core.Services.Shared
{
    using Commander.Core.Commons;

    /// <summary>
    /// Smart singleton pattern implementation
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class Singleton<T>
    {
        private Singleton()
        {
            
        }

        public static T Instance
        {
            get
            {
                return Nested.s_instance;
            }
        }

        static class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
                if (E.Container.Has<T>())
                    s_instance = E.Container.Get<T>();
                else
                    s_instance = InstanceFactory.Create<T>();
            }

            internal static readonly T s_instance;
        }
    }
}
